package com.demo.cms.webapp.template.directive;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.demo.cms.model.Category;
import com.demo.cms.service.CategoryService;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.webapp.views.freemarker.directive.core.PageContextDirectiveSupport;

import freemarker.core.Environment;
import freemarker.core.Environment.TemplateDirectiveBodyEx;
import freemarker.template.SimpleHash;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

/**
 * 栏目列表指令
 * 通过父栏目ID查询下级栏目信息
 * @author ryuu.kk
 *
 */
public class CategoryListDirective extends PageContextDirectiveSupport {
	
	/**
	 * 页面模板名
	 */
	private static final String CATEGORY_LIST_TEMPLATE_NAME = "classpath:/com/demo/cms/webapp/template/categoryList.ftl";
	
	/**
	 * 栏目数据服务
	 */
	@Autowired
	private CategoryService categoryService;

	@SuppressWarnings("rawtypes")
	@Override
	public void preWriter(Environment env, TemplateDirectiveBodyEx bodyEx,
			TemplateModel[] loopVars, SimpleHash hash, Map params)
			throws IOException, TemplateException {
		
		// 栏目模型
		List<Category> categoryList = new ArrayList<Category>(0);
		// 栏目父ID
		Long pid = null;
		Long channelId = null;
		Integer limit;
		Integer sort;
		// 是否查询同辈节点
		boolean sibling;
		
		pid = getLong("id", params);
		sibling = getBoolean("sibling", params, Boolean.valueOf(getDefaultValue("sibling")));
		if (sibling) {
			// 查询同辈数据
			Category root = categoryService.find(pid);
			if (root == null) {
				throw new TemplateException("找不到ID=" + pid + "的栏目", env);
			}
			pid = root.getPid();
		}
		// 取得检索大小
		limit = getInteger("limit", params, Integer.parseInt(getDefaultValue("limit")));
		// 取得排序类型
		sort = getInteger("sort", params, Integer.parseInt(getDefaultValue("sort")));
		SearchArgumentSupport args = new SearchArgumentSupport(0, limit);
		
		switch (sort) {
		// 排序方式  1:ID升序(默认); 2:ID降序; 3:名称升序; 4:名称降序
		case 1:
			// 栏目ID升序
			args.setOrders(new String[]{" ID ASC "});
			break;
		case 2:
			// 栏目ID降序
			args.setOrders(new String[]{" ID DESC "});
			break;
		case 3:
			args.setOrders(new String[]{" NAME ASC "});
			break;
		case 4:
			args.setOrders(new String[]{" NAME DESC "});
			break;
		default:
			// 栏目ID顺序
			args.setOrders(new String[]{"ID"});
		}
		
		// 栏目列表查询
		ResultSearchResult<Category> results = categoryService.search(channelId, pid, args);
		if (!results.isEmpty()) {
			categoryList = new ArrayList<Category>(results.getResults());
		}
		// 栏目&文章列表
		hash.put("categoryList", categoryList);
	}

	@Override
	public void afterInvoke(Writer writer) {
		
	}

	@SuppressWarnings("rawtypes")
	@Override
	public String getTemplatePath(Map params) {
		String path = "";
		try {
			path = getString("path", params, CATEGORY_LIST_TEMPLATE_NAME);
		} catch (TemplateModelException e) {}
		return path;
	}
}
